# Clean up
rm(list = ls())

# Load necessary packages
library(rdd)
library(foreign)
library(tidyverse) 
library(readstata13)
library(gridExtra)
library(psych)
library(readxl)
library(rdrobust)
library(viridis)
library(psych)

# Set language to English
Sys.setenv(LANG = "en")

# Set working directory
setwd("~/Dropbox/Referendums/")

# FIGURE 1
# Import data
referendums_data <- read.dta13("data/referendums_data/data_plus_vparty.dta")

referendums_plot_minorities <- referendums_data %>%
  mutate(Party = ifelse(rad_right_party == 1, "Radical-right parties", "Other parties")) %>%
  ggplot(aes(x = own_party_position_surplus, y = minority_groups, color = Party, linetype = Party)) +
  geom_point(alpha = 0.1) + 
  scale_color_manual(values = c("black", "blue")) +
  stat_smooth(method = "lm") +
  theme_bw() +
  labs(x = "Electoral surplus on party's position", y = "Positive mentions of minorities in party's subsequent manifesto")
referendums_plot_minorities

## FIGURE 2
# Import dataset with results
first_others <- read.dta13("results/first_others.dta")

# Plot
first_others_plot <- first_others %>%
  filter(Errors == "Clustered") %>%
  mutate(ci_upper95 = coef + 1.96 * stderr) %>%
  mutate(ci_lower95 = coef - 1.96 * stderr) %>%
  mutate(ci_upper90 = coef + 1.645 * stderr) %>%
  mutate(ci_lower90 = coef - 1.645 * stderr) %>%
  mutate(Topic = fct_reorder(Election, coef, .desc = T)) %>%
  ggplot(aes(x = Election, y = coef)) +
  geom_linerange(aes(ymin = ci_lower, ymax = ci_upper), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "red") + 
  theme_bw() +
  xlab(" ") + 
  ylab("Difference in the effect of electoral surplus for radical-right and other parties") + 
  ggtitle(" ")
first_others_plot

## FIGURE 3
# Import dataset with results
results_by_topic <- read.dta13("results/by_topic.dta")

results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_aggriculture"] <-  "Agriculture"
results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_culture"] <-  "Culture, Religion, Media"
results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_economy"] <-  "Economy"
results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_education"] <-  "Education and Research"
results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_energy"] <-  "Energy"
results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_environment"] <-  "Environment"
results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_foreign"] <-  "Foreign Politics"
results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_finance"] <-  "Public Finances"
results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_security"] <-  "Security Politics"
results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_socpolicy"] <-  "Social Policy"
results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_stateorg"] <-  "State Organisation"
results_by_topic$Topic[results_by_topic$Topic == "own_pos_surplus_traffic"] <-  "Traffic and Infrastructure"

# Make the  plot
by_topic <- results_by_topic %>%
  filter(Topic != "fake") %>%
  mutate(by_hand = ifelse(Topic == "Minorities (hand coded)", "Category hand coded", "Category from Swissvotes")) %>%
  mutate(ci_upper95 = coef + 1.96 * stderr) %>%
  mutate(ci_lower95 = coef - 1.96 * stderr) %>%
  mutate(ci_upper90 = coef + 1.645 * stderr) %>%
  mutate(ci_lower90 = coef - 1.645 * stderr) %>%
  mutate(Topic = fct_reorder(Topic, coef, .desc = T)) %>%
  ggplot(aes(x = Topic, y = coef, color = by_hand)) +
  geom_linerange(aes(ymin = ci_lower95, ymax = ci_upper95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower90, ymax = ci_upper90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "red") + 
  scale_color_manual(values = c("black", "blue")) +
  coord_flip() + 
  theme_bw() +
  facet_grid(~Errors) +
  xlab("Topic of referendum") + 
  ylab("Difference in the effect of electoral surplus for radical-right and other parties") + 
  # Remove legend
  theme(legend.position = "none") +
  ggtitle(" ")
by_topic

# FIGURE B1
# Import data
referendums_data <- read.dta13("data/referendums_reported_vote.dta")

reported_vote_df <- referendums_data %>%
  filter(country == "CHE") %>%
  filter(rad_right == 1) %>%
  select(official_vote, vote_cses)

col_means <- colMeans(reported_vote_df[1:2])
col_meansdf <- stack(col_means)

reported_vote_plot <- ggplot(col_meansdf, aes(x = ind, y = values)) + 
  geom_col(fill = "white", color = "black") +
  theme_bw() +
  scale_x_discrete(name = " ", labels = c("Official vote", "Vote as declared in\npost-electoral surveys\n(CSES)")) +
  scale_y_continuous(name = " ", limits = c(0, 10))
reported_vote_plot

# FIGURE B2
# Import data
referendums_data <- read.dta13("data/referendums_reported_vote.dta")

reported_vote_df <- referendums_data %>%
  filter(country == "CHE") %>%
  filter(rad_right == 0) %>%
  select(official_vote, vote_cses)

col_means <- colMeans(reported_vote_df[1:2])
col_meansdf <- stack(col_means)

reported_vote_plot_others <- ggplot(col_meansdf, aes(x = ind, y = values)) + 
  geom_col(fill = "white", color = "black") +
  theme_bw() +
  scale_x_discrete(name = " ", labels = c("Official vote", "Vote as declared in\npost-electoral surveys\n(CSES)")) +
  scale_y_continuous(name = " ", limits = c(0, 10))
reported_vote_plot_others

# FIGURE B3
# Import data
referendums_data <- read.dta13("data/referendums_data/data_plus_vparty.dta")

# Plot
distribution_plot <- ggplot(referendums_data, aes(x = minority_groups)) + 
  geom_density(color="darkblue", fill="lightblue")  +
  theme_bw() +
  labs(y = "Density", x = "Positive mentions of minorities in party's manifesto")
distribution_plot

# FIGURE B4
# Import data
referendums_data <- read.dta13("data/referendums_data/data_plus_vparty.dta")

# Plot
descriptive_matrix <- describeBy(referendums_data$own_party_position_surplus, list(referendums_data$rad_right_party), 
                                 mat = TRUE, digits = 2)

names(descriptive_matrix) [names(descriptive_matrix) == 'group1'] = 'rrp'

descriptive_matrix$se = descriptive_matrix$sd/ (sqrt (descriptive_matrix$n))

limits = aes (ymax = mean + (1.96 * se), ymin = mean - (1.96 * se))

elecsurplus_descriptive_plot <- descriptive_matrix %>%
  mutate(Party = ifelse(rrp == 1, 'Radical right parties', 'Other parties')) %>%
  ggplot (aes(x = Party, y = mean, color = Party, fill = Party, linetype = Party)) +
  geom_bar (stat = 'identity', position = position_dodge(width = 0.5), alpha = 0.6) +
  geom_errorbar (limits, position = position_dodge(width = 0.9), width = 0) +
  ylab ('Electoral surplus') +
  xlab (' ') +
  scale_fill_manual(values = c("white", "white")) +
  scale_color_manual(values = c("black", "blue")) +
  theme_bw() +
  theme(legend.position = "none") 
elecsurplus_descriptive_plot

# FIGURE B5
# Import data
referendums_data <- read.dta13("data/referendums_data/data_plus_vparty.dta")

# Plot
referendums_plot_minorities_loess <- referendums_data %>%
  mutate(Party = ifelse(rad_right_party == 1, "Radical-right parties", "Other parties")) %>%
  ggplot(aes(x = own_party_position_surplus, y = minority_groups, color = Party, linetype = Party)) +
  geom_point(alpha = 0.1) + 
  scale_color_manual(values = c("black", "blue")) +
  stat_smooth(method = "loess", span = 1.5, size = 1, se = T) +
  theme_bw() +
  labs(x = "Electoral surplus on party's position", y = "Positive mentions of minorities in party's subsequent manifesto")
referendums_plot_minorities_loess

# FIGURE B6

# Import dataset with results
jackknives_dates <- read.dta13("results/jk_dates.dta")

# Recode date variable to have year only

jackknives_dates$Date[jackknives_dates$Date == 198310] <-  1983
jackknives_dates$Date[jackknives_dates$Date == 198710] <-  1987
jackknives_dates$Date[jackknives_dates$Date == 199110] <-  1991
jackknives_dates$Date[jackknives_dates$Date == 199510] <-  1995
jackknives_dates$Date[jackknives_dates$Date == 199910] <-  1999
jackknives_dates$Date[jackknives_dates$Date == 200310] <-  2003
jackknives_dates$Date[jackknives_dates$Date == 200710] <-  2007
jackknives_dates$Date[jackknives_dates$Date == 201110] <-  2011
jackknives_dates$Date[jackknives_dates$Date == 201510] <-  2015
jackknives_dates$Date[jackknives_dates$Date == 201910] <-  2019

# Plot
jk_plot_dates <- jackknives_dates %>%
  filter(Errors != "fake") %>%
  mutate(ci_upper95 = coef + 1.96 * stderr) %>%
  mutate(ci_lower95 = coef - 1.96 * stderr) %>%
  mutate(ci_upper90 = coef + 1.645 * stderr) %>%
  mutate(ci_lower90 = coef - 1.645 * stderr) %>%
  ggplot(aes(x = Date, y = coef)) +
  geom_linerange(aes(ymin = ci_lower95, ymax = ci_upper95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower90, ymax = ci_upper90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "red") + 
  coord_flip() + 
  theme_bw() +
  xlab("Year of election removed") + 
  ylab("Interaction coefficient for Electoral surplus x Rad right party") + 
  facet_grid(~Errors) +
  ggtitle(" ")
jk_plot_dates

# FIGURE B7
# Import dataset with results
jackknives_parties <- read.dta13("results/jk_parties.dta")

# Fix party variable to have the actual party name instead of its code
jackknives_parties$Party[jackknives_parties$Party == 43110] <-  "Greens"
jackknives_parties$Party[jackknives_parties$Party == 43120] <-  "Green Liberal Party"
jackknives_parties$Party[jackknives_parties$Party == 43220] <-  "Swiss Labour Party"
jackknives_parties$Party[jackknives_parties$Party == 43320] <-  "Social Democratic Party of Switzerland"
jackknives_parties$Party[jackknives_parties$Party == 43321] <-  "Independents’ Alliance"
jackknives_parties$Party[jackknives_parties$Party == 43420] <-  "FDP.The Liberals"
jackknives_parties$Party[jackknives_parties$Party == 43520] <-  "Christian Democratic People’s Party of Switzerland"
jackknives_parties$Party[jackknives_parties$Party == 43530] <-  "Protestant People’s Party of Switzerland"
jackknives_parties$Party[jackknives_parties$Party == 43531] <-  "Liberal 4 Party of Switzerland"
jackknives_parties$Party[jackknives_parties$Party == 43540] <-  "Christian Social Party"
jackknives_parties$Party[jackknives_parties$Party == 43710] <-  "Swiss Democrats"
jackknives_parties$Party[jackknives_parties$Party == 43711] <-  "Federal Democratic Union"
jackknives_parties$Party[jackknives_parties$Party == 43810] <-  "Swiss People’s Party"
jackknives_parties$Party[jackknives_parties$Party == 43811] <-  "Conservative Democratic Party of Switzerland"
jackknives_parties$Party[jackknives_parties$Party == 43901] <-  "Ticino League"
jackknives_parties$Party[jackknives_parties$Party == 43951] <-  "Freedom Party of Switzerland"

# Plot
jk_plot_parties <- jackknives_parties %>%
  filter(Errors != "fake") %>%
  mutate(ci_upper95 = coef + 1.96 * stderr) %>%
  mutate(ci_lower95 = coef - 1.96 * stderr) %>%
  mutate(ci_upper90 = coef + 1.645 * stderr) %>%
  mutate(ci_lower90 = coef - 1.645 * stderr) %>%
  ggplot(aes(x = Party, y = coef)) +
  geom_linerange(aes(ymin = ci_lower95, ymax = ci_upper95), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_linerange(aes(ymin = ci_lower90, ymax = ci_upper90), position = position_dodge(width = 0.75), size = 1) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "red") + 
  coord_flip() + 
  theme_bw() +
  xlab("Party removed") + 
  ylab("Interaction coefficient for Electoral surplus x Rad right party") + 
  facet_grid(~Errors) +
  ggtitle(" ")
jk_plot_parties

# FIGURE B8
# Import dataset with results
first_others <- read.dta13("results/first_others.dta")

# Plot
first_others_plot_bootstrap <- first_others %>%
  filter(Errors == "Bootstrapped standard errors") %>%
  mutate(ci_upper95 = coef + 1.96 * stderr) %>%
  mutate(ci_lower95 = coef - 1.96 * stderr) %>%
  mutate(ci_upper90 = coef + 1.645 * stderr) %>%
  mutate(ci_lower90 = coef - 1.645 * stderr) %>%
  mutate(Topic = fct_reorder(Election, coef, .desc = T)) %>%
  ggplot(aes(x = Election, y = coef)) +
  geom_linerange(aes(ymin = ci_lower, ymax = ci_upper), position = position_dodge(width = 0.75), size = 0.5) + 
  geom_point(size = 1.25, position = position_dodge(width = 0.75) ) + 
  geom_hline(aes(yintercept = 0), linetype = 2, color = "red") + 
  theme_bw() +
  xlab(" ") + 
  ylab("Difference in the effect of electoral surplus for radical-right and other parties") + 
  ggtitle(" ")
first_others_plot_bootstrap
